package com.cf.r1032_ak;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class d {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        in.nextToken(); int t = (int) in.nval;
        while (t-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        in.nextToken(); int n = (int) in.nval;
        a = new int[n];
        b = new int[n];
        for (int i = 0; i < n; i++) {
            in.nextToken(); a[i] = (int) in.nval - 1;
        }
        for (int i = 0; i < n; i++) {
            in.nextToken(); b[i] = (int) in.nval - 1;
        }
         ans = new ArrayList<>();

        for (int cur = 0; cur < 2 * n; cur++) {
            for (int i = 0; i < n; i++) {
                if (a[i] == cur) {
                    if (cur % 2 == 0) {
                        for (int j = i - 1; j >= cur / 2; j--) move(1, j);
                    } else {
                        move(3, i);
                        for (int j = i - 1; j >= cur / 2; j--) move(2, j);
                    }
                    break;
                } else if (b[i] == cur) {
                    if (cur % 2 == 0) {
                        move(3, i);
                        for (int j = i - 1; j >= cur / 2; j--) move(1, j);
                    } else {
                        for (int j = i - 1; j >= cur / 2; j--) move(2, j);
                    }
                    break;
                }
            }
        }
        out.println(ans.size());
        for (int[] a : ans) {
            out.println(a[0] + " " + (a[1] + 1));
        }
    }

    static List<int[]> ans;
    static int[] a;
    static int[] b;

    static void move(int t, int x) {
        ans.add(new int[]{t, x});
        if (t == 3) swapab(x);
        else if (t == 1) swap(a, x, x + 1);
        else swap(b, x, x + 1);
    }

    static void swapab(int x) {
        int tmp = a[x];
        a[x] = b[x];
        b[x] = tmp;
    }

    static void swap(int[] arr, int x, int y) {
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
}
